package com.nengchuan.eic.nceconomy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.nengchuan.eic.nceconomy.dao.SwDirectoryMapper;
import com.nengchuan.eic.nceconomy.entity.dto.SwDirectoryPageDTO;
import com.nengchuan.eic.nceconomy.entity.po.SwDirectory;
import com.nengchuan.eic.nceconomy.entity.vo.SwDirectoryVO;
import com.nengchuan.util.MapperUtils;
import com.nengchuan.util.NullCheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 三维目录表 nc_sw_directory 服务实现类
 * </p>
 *
 * @author panwen
 * @since 2020-05-25
 */
@Service
public class SwDirectoryServiceImpl extends ServiceImpl<SwDirectoryMapper, SwDirectory> implements SwDirectoryService {
    @Autowired
    private SwDirectoryMapper swDirectoryMapper;


    @Override
    public PageInfo<SwDirectoryVO> page(SwDirectoryPageDTO dto){

        LambdaQueryWrapper<SwDirectory> queryWrapper=new QueryWrapper<SwDirectory>().lambda();
        if(NullCheckUtils.isNull(dto.getParentId())){
            queryWrapper.isNull(SwDirectory::getParentId);
        }else{
            queryWrapper.eq(SwDirectory::getParentId,dto.getParentId());
        }

        if(NullCheckUtils.isNotNull(dto.getDirName())){
            queryWrapper.like(SwDirectory::getDirName,dto.getDirName());
        }

        queryWrapper.orderByDesc(true,  SwDirectory::getSort);
        Page<SwDirectory> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        IPage<SwDirectory> swDirectoryIPage=this.page(page,queryWrapper);
        List<SwDirectory> records=page.getRecords();
        PageInfo<SwDirectoryVO> pageInfo = new PageInfo<>();

        if(NullCheckUtils.isNotNull(records)) {
            pageInfo.setPageNum((int) swDirectoryIPage.getCurrent());
            pageInfo.setPageSize((int) swDirectoryIPage.getSize());
            pageInfo.setTotal(swDirectoryIPage.getTotal());
            pageInfo.setList(MapperUtils.mappingList(records, SwDirectoryVO.class));
        }
        return pageInfo;
    }
}
